package com.hbk.codemaker;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.hbk.programe.Field;

public class FieldPanel extends JPanel implements MouseListener{
	
	public static final int FIELD_HEIGHT=142;
	
	private MainFrame window;
		
	private JLabel typeLabel=new JLabel("类型");
	
	private JTextField typeField=new JTextField();
	
	private JLabel nameLabel=new JLabel("声明");
	
	private JTextField nameField=new JTextField();
	
	private JLabel cloumnLabel=new JLabel("列名");
	
	private JTextField cloumnField=new JTextField();

	private JLabel fetchLabel=new JLabel("关系");
	
	private JTextField fetchField=new JTextField();

	private JLabel commentLabel=new JLabel("注释");
	
	private JTextField commentField=new JTextField();
	
	private JLabel constantsLabel=new JLabel("常量");
	
	private ConstantsPanel constantsPanel=new ConstantsPanel();
	
	public FieldPanel(int width,MainFrame frame) {
		window=frame;
		setLayout(null);
		
		typeLabel.setBounds(0, 0, 30, 32);
		typeField.setBounds(30+10, 0, 150, 32);
		nameLabel.setBounds(30+10+150+10, 0, 30, 32);
		nameField.setBounds(30+10+150+10+30+10, 0, 150, 32);
		
		cloumnLabel.setBounds(30+10+150+10+30+10+150+10, 0, 30, 32);
		cloumnField.setBounds(30+10+150+10+30+10+150+10+30+10, 0, 150, 32);
		fetchLabel.setBounds(30+10+150+10+30+10+150+10+30+10+150+10, 0, 30, 32);
		fetchField.setBounds(30+10+150+10+30+10+150+10+30+10+150+10+30+10, 0, 150, 32);
		
		commentLabel.setBounds(0, 40, 30, 32);
		commentField.setBounds(30+10, 40, 150, 32);
		constantsLabel.setBounds(30+10+150+10, 40, 30, 32);
		
		constantsPanel.setBounds(30+10+150+10+30+10, 40, 550, 100);
		
		addMouseListener(this);
		
		add(typeLabel);
		add(typeField);
		add(nameLabel);
		add(nameField);
		add(cloumnLabel);
		add(cloumnField);
		add(fetchLabel);
		add(fetchField);
		add(commentLabel);
		add(commentField);
		add(constantsLabel);
		add(constantsPanel);
		
		setPreferredSize(new Dimension(width, FIELD_HEIGHT));
		setBackground(Color.WHITE);
	}
	
	public void setField(Field field) {
		typeField.setText(field.getType());
		nameField.setText(field.getName());
		cloumnField.setText(field.getColumn());
		fetchField.setText(field.getFetch());
		commentField.setText(field.getComment());
		constantsPanel.setConstants(field.getConstant());
	}

	public void repaint(int width) {
		setPreferredSize(new Dimension(width, FIELD_HEIGHT));
		repaint();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		
		
	}

	@Override
	public void mousePressed(MouseEvent e) {
		System.out.println("gained");
		e.getComponent().setBackground(new Color(151,227,235));
		if(e.getComponent() instanceof JTextField) {
			window.setLastFocusComponent(e.getComponent());
		}else {
			window.setLastFocusComponent(this);
		}
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		System.out.println("lost");
		e.getComponent().setBackground(new Color(255,255,255));
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	

}
